var user = require('../models/user.js');





exports.showUser = function(req,res){
	if (req.url == '/favicon.ico') {return;}
	var arr = [];
	user.select(arr,function(err,results){
		if (err) {
			res.end('查询失败');
		}else if(results!=undefined){
			res.render('index',{ data : results });
		}else{
			res.end('ERROR');
		}
	});
}


exports.add = function(req,res){
	if (req.url == '/favicon.ico') {return;}
	var name = req.body.name;
	var sex = req.body.sex;
	var age = req.body.age;
	var province = req.body.province;
	var arr = [name,sex,age,province];

	user.insert(arr,function(err,results){
		if (err) {
			res.end('添加失败');
		}else if(results!=undefined && results!=null){
			res.end('<script>location.href="/";</script>');
		}else{
			res.end('ERROR');
		}
	});
}



exports.edit = function(req,res){
	if (req.url == '/favicon.ico') {return;}
	var name = req.body.name;
	var sex = req.body.sex;
	var age = req.body.age;
	var province = req.body.province;
	var id = req.body.id;
	var arr = [name,sex,age,province,id];
	
	user.update(arr,function(err,results){
		if (err) {
			res.end('修改失败');
		}else if(results!=undefined && results!=null){
			res.end('<script>location.href="/";</script>');
		}else{
			res.end('ERROR');
		}
	});
}


exports.del = function(req,res){
	if (req.url == '/favicon.ico') {return;}
	var id = req.params.id;
	var arr = [id];
	user.delete(arr,function(err,results){
		if (err) {
			res.end('修改失败');
		}else if(results!=undefined && results!=null){
			res.end('<script>location.href="/";</script>');
		}else{
			res.end('ERROR');
		}
	});
}